import * as express from "express";
import { wrap } from "../../common/exceptions.js";
import authorizeRequest from "../../middlewares/authorizeRequest.middleware.js";
import ROLE from "../users/role.model.js";
import accountController from "./account.controller.js";

// Logs roles at setup time, and logs path at request time before calling authorizeRequest
function logAndWrapAuthorize(roles) {
  console.log(`[Route Setup] Required Roles: ${roles.map(r => r.name || r).join(", ")}`);
  return wrap((req, res, next) => {
    console.log(`[Route Access] Path: ${req.originalUrl}`);
    return authorizeRequest(roles)(req, res, next);
  });
}

export default express
  .Router()
  .get("/:id", [
    logAndWrapAuthorize([ROLE.ADMIN, ROLE.SUPER_ADMIN]),
    wrap(accountController.byId),
  ])
  .put("/:id", [
    logAndWrapAuthorize([ROLE.ADMIN, ROLE.SUPER_ADMIN]),
    wrap(accountController.update),
  ])
  // New endpoint for setting trial periods - restricted to SUPER_ADMIN only
  .put("/:id/trial", [
    logAndWrapAuthorize([ROLE.SUPER_ADMIN]),
    wrap(accountController.setTrialPeriod),
  ]);